﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Wave;
using System.Windows;

namespace HPKaraoke
{
    /// <summary>
    /// Lớp phụ trách mảng giao diện của các bài hát lấy dữ liệu từ file xml
    /// </summary>
    public class XmlSongGUI : SongGUI
    {

        /// <summary>
        /// Control để phát nhạc
        /// </summary>
        public IWavePlayer beatPlayer { get; set; }

        public XmlSongGUI(string parentPath, string configFileName)
        {
            song = new XmlSongController(parentPath, configFileName);
            this.beatPlayer = new WaveOut();
        }


        public override void Play()
        {
            if (!this.song.WasPlayed)
            {
                this.song.LoadInputStream();
                if (this.beatPlayer != null)
                    this.beatPlayer.Init(song.InputStream as WaveStream);
            }
            this.beatPlayer.Play();
            base.Play();
        }

        public override void PauseOrResume()
        {
            base.PauseOrResume();
        }

        public override void Stop()
        {
            base.Stop();
        }

        public override void Update(object sender, EventArgs e)
        {
            base.Update(sender, e);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.beatPlayer != null)
                {
                    this.beatPlayer.Stop();
                }
                if (song.InputStream != null)
                {
                    // this one really closes the file and ACM conversion
                    song.Close();
                    // this one does the metering stream
                    (song.InputStream as WaveStream).Close();
                    song.InputStream = null;
                }
                if (this.beatPlayer != null)
                {
                    this.beatPlayer.Dispose();
                    this.beatPlayer = null;
                }
            }
        }

        public override int GetPlaybackState()
        {
            return (int)this.beatPlayer.PlaybackState;
        }
    }

}
